﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FWMS.DAO
{
    class CailiaoguanliDao
    {
        //查询全部
        public Bean.CailiaoguanliBean cailiaoguanliDaoSelect()
        {
            DB.DBbase dbbase = new DB.DBbase();
            Bean.CailiaoguanliBean bean = new FWMS.Bean.CailiaoguanliBean();
            string sql = "";
            sql = "select * from cailiaoguanli order by cailiaoname";
            if (dbbase.ExecuteSQL(sql.Trim()))
            {
                if (dbbase.rows.Length != 0)
                {
                    bean.Selectresult = dbbase.rows;
                    bean.Resultclo = dbbase.Col_Number;
                    bean.Resultrow = dbbase.Row_Number;
                }
                else
                {
                    bean.Selectresult = null;
                }
            }
            else
            {
                bean.Selectresult = null;
            }
            return bean;
        }
        //条件查询
        public Bean.CailiaoguanliBean cailiaoguanliDaoSelectCondition(Bean.CailiaoguanliBean bean)
        {
            bool cailiaonameflag = false;
            DB.DBbase dbbase = new DB.DBbase();
            string sql = "";
            sql = "select * from cailiaoguanli where ";
            if (!"".Equals(bean.Cailiaoname))
            {
                sql = sql + "cailiaoname LIKE '%" + bean.Cailiaoname + "%'";
                cailiaonameflag = true;
            }
            if ((!"".Equals(bean.Cailiaotype)) && (bean.Cailiaotype != null))
            {
                if (cailiaonameflag)
                {
                    sql = sql + " and cailiaotype LIKE '%" + bean.Cailiaotype + "%'";
                }
                else
                {
                    sql = sql + " cailiaotype LIKE '%" + bean.Cailiaotype + "%'";
                }
            }
            sql = sql + " order by cailiaoname";
            if (dbbase.ExecuteSQL(sql.Trim()))
            {
                if (dbbase.rows.Length != 0)
                {
                    bean.Selectresult = dbbase.rows;
                    bean.Resultclo = dbbase.Col_Number;
                    bean.Resultrow = dbbase.Row_Number;
                }
                else
                {
                    bean.Selectresult = null;
                }
            }
            else
            {
                bean.Selectresult = null;
            }
            return bean;
        }

        //添加
        public bool cailiaoguanliDaoAdd(Bean.CailiaoguanliBean bean)
        {
            DB.DBbase dbbase = new DB.DBbase();
            string sql = ""; // as user 
            sql = "insert into cailiaoguanli values('" + COMMAND.Command.add50(bean.Resultrow.ToString()) + "','" + bean.Cailiaoname + "','" + bean.Cailiaotype + "')";
            if (dbbase.ExecuteSQL(sql.Trim()))
            {
                if (dbbase.command())
                {
                    return true;
                }
                else
                {
                    return false;
                }

            }
            else
            {
                return false;
            }
        }

        //修改
        public bool cailiaoguanliDaoModify(Bean.CailiaoguanliBean bean)
        {
            DB.DBbase dbbase = new DB.DBbase();
            string sql = "";
            sql = "update cailiaoguanli set cailiaoname = '" + bean.Cailiaoname.Trim() + "',cailiaotype = '" + bean.Cailiaotype.Trim() + "' where id='" + bean.Id.Trim() + "'";
            if (dbbase.ExecuteSQL(sql.Trim()))
            {
                if (dbbase.command())
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
    }
}
